public class Darr {
    private Object [] data = null;
    private int len = 0;
    private int travelCount = 0;

    public Darr() {}

    //添加数据到动态数组
    public Darr add(Object ... inputData) {
        int ilen = inputData.length;
        if (ilen == 0)
            return this;

        Object [] newData = new Object[this.len + ilen];
        if (this.data != null) {
            System.arraycopy(this.data, 0, newData, 0, this.len);
        }
        System.arraycopy(inputData, 0, newData, this.len, ilen);
        this.len += ilen;
        this.data = newData;

        return this;
    }


    /*
     *                     查找到起始位置 个数
     * 查第一个              1              1
     * 查第二个              2              1
     * 查前几个              1              3
     * 查从第一个到第三个    1              3
     * 查最后一个            -1             1
     * 查后几个              -1             3
     * 查全部                0              0
     *  起始位置: 从1开始, 从左向右查找
     *  起始位置: 从-1开始, 从右向左查找
     *  个数和起始位置为0: 查找全部
     *  ind控制是否数组下标
     */
    private Darr _find(Object key, int start, int num,
                                    boolean ind, InterfaceDarrCmp cmpObj) {
        Darr findData = new Darr();
        boolean flags = false;
        int index;

        //如果起始值为负数，取绝对值，置位标识位
        if (start < 0) {
            start *= -1;
            flags = true;
        }
        num = (num <= 0) ? len : num;

        for (int i = 0; i < len; ++i) {
            index = flags ? len - i - 1 : i;
            if ((cmpObj == null && data[index].equals(key))
                    || (cmpObj != null && cmpObj.eq(key, data[index]))) {
                if (start != 0 && --start > 0) {
                    continue;
                }

                if (num > 0) {
                    if (ind) {
                        findData.add(index);
                    } else {
                        findData.add(data[index]);
                    }
                    if (--num == 0)
                        break;
                }
            }
        }

        return (findData.length() == 0) ? null : findData;
    }

    public Darr find(Object key, int start, int num, InterfaceDarrCmp cmpObj) {
        return _find(key, start, num, false, cmpObj);
    }

    public Darr find(Object key, int start, int num) {
        return _find(key, start, num, false, null);
    }

    public Darr find(Object key) {
        return find(key, null);
    }

    public Darr find(Object key, InterfaceDarrCmp cmpObj) {
        /*
         * if (cmpObj == null) {
         *     for (int i = 0; i < len; ++i) {
         *         if (data[i].equals(key))
         *             return data[i];
         *     }
         * } else {
         *     for (int i = 0; i < len; ++i) {
         *         if (cmpObj.eq(key, data[i]))
         *             return data[i];
         *     }
         * }
         */

        return find(key, 0, 0, cmpObj);
    }

    //查找第几个或倒数第几个数据元素
    public Object find(Object key, int num, InterfaceDarrCmp cmpObj) {
        Darr findData = find(key, num, 1, cmpObj);
        if (findData != null) {
            return findData.value();
        }
        return null;
    }

    //查找第几个或倒数第几个数据元素
    public Object find(Object key, int num) {
        return find(key, num, 1, null);
    }

    //返回查找到数据的下标数组
    public Darr findInd(Object key, int start, int num,
                                InterfaceDarrCmp cmpObj) {
        return _find(key, start, num, true, cmpObj);
    }

    public int findInd(Object key, int num, InterfaceDarrCmp cmpObj) {
        Darr findData = findInd(key, num, 1, cmpObj);
        if (findData != null) {
            return (int)findData.value();
        }
        return -1;
    }

    public int findInd(Object key, int num) {
        return findInd(key, num, null);
    }

    // 动态数组元素的按下标删除
    public Darr remove(int index) {
        if (index < 0 || index >= len)
            return this;

        Object [] newData = new Object[len - 1];

        System.arraycopy(data, 0, newData, 0, index);
        System.arraycopy(data, index + 1, newData, index, len - index - 1);
        len--;
        data = newData;

        return this;
    }

    //批量删除
    //num <= 0 代表删除到结束
    public Darr remove(int start, int num) {
        if (start < 0 || start >= len)
            return this;

        num = (num <= 0) ? len : num;

        while (num-- != 0) {
            remove(start);
        }

        return this;
    }

    public Darr remove(Object key, int start, int num,
                            InterfaceDarrCmp cmpObj) {
        boolean flags = false;
        int index;

        //如果起始值为负数，取绝对值，置位标识位
        if (start < 0) {
            start *= -1;
            flags = true;
        }
        num = (num <= 0) ? len : num;

        for (int i = 0; i < len; ++i) {
            index = flags ? len - i - 1 : i;
            if ((cmpObj == null && data[index].equals(key))
                    || (cmpObj != null && cmpObj.eq(key, data[index]))) {
                if (start != 0 && --start > 0) {
                    continue;
                }

                if (num > 0) {
                    remove(index);
                    i--;
                    if (--num == 0)
                        break;
                }
            }
        }

        return this;
    }

    public Darr remove(Object key, int start, int num) {
        return remove(key, start, num, null);
    }

    public Darr remove(Object key, int num) {
        return remove(key, num, 1, null);
    }

    public Darr remove(Object key) {
        return remove(key, 0, 0, null);
    }

    // 清空动态数组
    public Darr clear() {
        data = null;
        len = 0;
        return this;
    }

    //返回动态数组长度
    public int length() {
        return len;
    }

    //按下标返回动态数组的元素
    public Object ind(int index) {
        if (index < 0 || index >= len)
            return null;
        return data[index];
    }

    // 数组循环左移
    public Darr left() {
        Object t;

        for (int i = 0, l = len - 1; i < l; ++i) {
            t = data[i];
            data[i] = data[i + 1];
            data[i + 1] = t;
        }

        return this;
    }

    // 数组循环右移
    public Darr right() {
        Object t;

        for (int i = len - 1; i > 0; --i) {
            t = data[i];
            data[i] = data[i - 1];
            data[i - 1] = t;
        }

        return this;
    }

    //遍历计数器清0， 为了正常重新遍历
    public Darr rewind() {
        travelCount = 0;
        return this;
    }

    //遍历使用的条件
    public boolean next() {
        if (travelCount == len) {
            travelCount = 0;
            return false;
        }
        travelCount++;
        return true;
    }

    //返回当前遍历的数据元素
    public Object value() {
        return data[travelCount - 1];
    }
}
